var _ = require("underscore");
var Comment = require("../models/comment");

// 评论
exports.save = function (req, res) {
    var _comment = req.body.comment;
    var movieId = _comment.movie; 
    if(_comment.cid){ // 评论回复
        Comment.findById(_comment.cid,function (err,comment) {  
            var reply = {
                from : _comment.from,
                to : _comment.tid,
                content : _comment.content
            }

            comment.reply.push(reply)
            comment.save(function (err, comment) {
                if (err) {
                    console.log(err);
                }
                // console.log(comment);
                res.redirect("/movie/" + movieId);
            });
        })
    }else{ // 新建的对电影的评论
        var comment = new Comment(_comment);

        comment.save(function (err, comment) {
            if (err) {
                console.log(err);
            }
            // console.log(comment);
            res.redirect("/movie/" + movieId);
        });
    }
    
}

